<template>
  <div class="app-container">
    <el-row :gutter="20">
      <!--分类数据-->
      <el-col :span="24" :xs="24">

        <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="80px">
          <el-form-item label="分类名称" prop="categoryName">
            <el-input v-model="queryParams.categoryName" placeholder="请输入分类名称" size="small" clearable style="width: 150px"/>
          </el-form-item>
          <el-form-item>
            <el-button type="primary"  size="mini" icon="el-icon-search" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery('queryForm')">重置</el-button>
          </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="el-icon-plus" size="mini" v-hasPermi="['saveInfoCategory']"  @click="handleAdd">新增一级分类</el-button>
          </el-col>

          <right-toolbar :showSearch.sync="showSearch" @queryTable="queryInfoCategoryTreeResList" :columns="columns"></right-toolbar>
        </el-row>

        <el-table :stripe="true" v-loading="loading" :data="dataList" row-key="categoryId" :tree-props="{children: 'children', hasChildren: 'hasChildren'}">
          <el-table-column  prop="categoryName" label="分类名称" :show-overflow-tooltip="true">
            <template slot-scope="scope">
              <span style="margin-left: 10px">{{ scope.row.categoryName }}</span>
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center" width="250" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button size="mini" type="text" icon="el-icon-plus" v-hasPermi="['saveInfoCategory']"  @click="handleAdd(scope.row)">新增子级</el-button>
              <el-button size="mini" type="text" icon="el-icon-edit" v-hasPermi="['updateInfoCategory']"  @click="handleUpdate(scope.row)">修改分类</el-button>
              <el-button size="mini" type="text" icon="el-icon-delete" v-hasPermi="['deleteInfoCategory']"  @click="handleDelete(scope.row)">删除分类</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-col>
    </el-row>

    <!-- 添加或修改参数配置对话框 -->
    <el-dialog :title="title" :visible.sync="addUpdateOpen" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="addUpdateOpenRules"  label-width="80px">
        <el-form-item label="上级分类" prop="parentId">
          <treeselect v-model="form.parentId"
                      :options="infoCategoryOptions"
                      :defaultExpandLevel="10"
                      :normalizer="normalizerTree"
                      :show-count="true"
                      :hide-required-asterisk="true"
                      :disabled="form.categoryId === undefined && form.parentId !== -1" autocomplete="off"
                      placeholder="请选择上级分类" />
        </el-form-item>

        <el-form-item label="分类名称" prop="categoryName">
          <el-input :hide-required-asterisk="true" autocomplete="off" v-model="form.categoryName" placeholder="请输入分类名称" maxlength="20" />
        </el-form-item>

        <el-form-item label="排序" prop="sort">
          <el-input-number :hide-required-asterisk="true" v-model="form.sort" :min="1" :max="9999" label="排序" />
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button  size="medium" type="primary" @click="submitForm">确 定</el-button>
        <el-button  size="medium" @click="cancelAddUpdateOpen">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
  import {queryInfoCategoryTreeResList, querySelectInfoCategoryTreeResList, saveInfoCategory, updateInfoCategory, deleteInfoCategoryByCategoryId} from '@/api/info/infoCategory'
  import Treeselect from "@riophae/vue-treeselect";
  import "@riophae/vue-treeselect/dist/vue-treeselect.css";

  //默认查询参数
  let defaultQueryParams = {
    categoryName: undefined
  }
  export default {
    name: "infoCategoryAdmin",
    components: {Treeselect},
    data() {
      return {
        // 遮罩层
        loading: true,
        // 显示搜索条件
        showSearch: true,
        // table表格数据
        dataList: [],
        // 上级分类选择
        infoCategoryOptions: [],
        // 弹出层标题
        title: "",
        // 是否显示新增修改弹出层
        addUpdateOpen: false,
        // 增加修改表单参数
        form: {},
        // 查询参数
        queryParams: { ...defaultQueryParams },
        //列信息
        columns: [],
        // 新增修改表单校验
        addUpdateOpenRules: {
          categoryName: [
            { required: true, message: "分类名称不能为空",trigger: "blur" }
          ],
          sort: [
            { required: true, message: "排序不能为空",trigger: "blur" }
          ]
        }
      }
    },
    created() {
      this.queryInfoCategoryTreeResList();
    },
    methods: {
      /** 查询分类列表 */
      queryInfoCategoryTreeResList() {
        this.loading = true
        queryInfoCategoryTreeResList(this.queryParams).then(response => {
          this.dataList = response.data;
        }).finally(() =>{
          //关闭加载特效
          this.loading = false;
        });
      },

      /** 重置按钮操作 */
      resetQuery(fromId) {
        this.resetForm(fromId)
        this.handleQuery()
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.page = 1
        this.queryInfoCategoryTreeResList()
      },

      /** 查询一级分类列表 */
      querySelectInfoCategoryTreeResList() {
        querySelectInfoCategoryTreeResList({}).then(response => {
          this.infoCategoryOptions = [];
          const category = { categoryId: "-1", categoryName: '一级分类', children: [] };
          category.children = response.data;
          this.infoCategoryOptions.push(category);
        });
      },
      /** 转换分类数据结构 */
      normalizerTree(node) {
        if (node.children && !node.children.length) {
          delete node.children;
        }
        return {
          id: node.categoryId,
          label: node.categoryName,
          children: node.children
        };
      },
      /** 新增按钮操作 */
      handleAdd(row) {
        this.reset();
        //加载顶级分类
        this.querySelectInfoCategoryTreeResList();
        //修改表单填充
        if (row != null && row.categoryId) {
          this.form.parentId = row.categoryId;
        }else {
          this.form.parentId = "-1";
        }
        this.addUpdateOpen = true;
        this.title = "添加分类";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        //加载顶级分类
        this.querySelectInfoCategoryTreeResList();
        //修改表单填充
        this.form = {...row};
        this.addUpdateOpen = true;
        this.title = "修改分类";
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        this.$confirm('是否确认删除所选分类?', "警告", {
          cancelButtonText: "取消",
          confirmButtonText: "确定",
          type: "warning"
        }).then(function() {
          const categoryId={categoryId:row.categoryId}
          return deleteInfoCategoryByCategoryId(categoryId);
        }).then(response => {
          this.$modal.msgSuccess(response.message)
          this.queryInfoCategoryTreeResList();
        }).catch(function() {});
      },
      /** 提交保存或修改按钮 */
      submitForm: function() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.categoryId !== undefined) {
              updateInfoCategory(this.form).then(response => {
                this.$modal.msgSuccess(response.message)
                this.queryInfoCategoryTreeResList();
                this.addUpdateOpen = false
              });
            } else {
              saveInfoCategory(this.form).then(response => {
                this.$modal.msgSuccess(response.message)
                this.queryInfoCategoryTreeResList();
                this.addUpdateOpen = false
              });
            }
          }
        });
      },
      // 取消修改增加弹出层按钮
      cancelAddUpdateOpen() {
        this.addUpdateOpen = false
      },
      // 表单重置
      reset() {
        this.form = {
          categoryId: undefined,
          categoryName: undefined,
          parentId: "-1",
          sort: 1
        };
        this.resetForm('form')
      }
    }
  }
</script>
